<?php


define('IN_OS', true);

require(dirname(__FILE__) . '/includes/init.php');

if(!isset($_REQUEST['act']) || empty($_REQUEST['act'])){
	$_REQUEST['act'] = 'list';
}
if($_REQUEST['act'] == 'list'){
    $smarty->assign('ur_here',      '代销授权用户列表');
    $smarty->assign('full_page',    1);

	$list = get_agent_userlist();
	$smarty->assign('user_list',    $list['user_list']);
    
    $smarty->assign('filter',       $list['filter']);
    $smarty->assign('record_count', $list['record_count']);
    $smarty->assign('page_count',   $list['page_count']);

    assign_query_info();
    
    $smarty->display('agent_auth_list.tpl');	
}elseif($_REQUEST['act'] == 'allot'){
	
	admin_priv('allot_agent_auth');
	
    $smarty->assign('ur_here',      '代销用户授权');
    $smarty->assign('form_action',  'act_allot'); 
    $agent_auth = array(
        'auth_id'        => 0,
        'start_time'    => date('Y-m-d', time() + 86400),
        'end_time'      => date('Y-m-d', time() + 365 * 86400),
        'act_range'     => 0,
        'act_range_ext' => '',
        'act_type'      => 0,
        'act_type_ext'  => 0,
        'enabled'       => 1
    );

    $smarty->assign('agent_auth', $agent_auth); 

    assign_query_info();
    
    $smarty->display('agent_auth_allot.tpl');	
}elseif($_REQUEST['act'] == 'search'){
	
	check_authz_json('allot_agent_auth');
	
    include_once(ROOT_PATH . 'includes/cls_json.php');

    $json   = new JSON;
    $filter = $json->decode($_GET['JSON']);
    $filter->keyword = json_str_iconv($filter->keyword);
        
    if ($filter->act_range == 0)
    {
        $arr[0] = array(
            'id'   => 0,
            'name' => '代销授权范围是全部商品，不需要此操作'
        );
    }
    elseif ($filter->act_range == 1)
    {
        $sql = "SELECT cat_id AS id, cat_name AS name FROM " . $os->table('category') .
            " WHERE cat_name LIKE '%" . mysql_like_quote($filter->keyword) . "%' LIMIT 50";
        $arr = $db->getAll($sql);
    }
    elseif ($filter->act_range == 2)
    {
        $sql = "SELECT brand_id AS id, brand_name AS name FROM " . $os->table('brand') .
            " WHERE brand_name LIKE '%" . mysql_like_quote($filter->keyword) . "%' LIMIT 50";
        $arr = $db->getAll($sql);
    }
    else
    {
        $sql = "SELECT goods_id AS id, goods_name AS name FROM " . $os->table('goods') .
            " WHERE goods_name LIKE '%" . mysql_like_quote($filter->keyword) . "%'" .
            " OR goods_sn LIKE '%" . mysql_like_quote($filter->keyword) . "%' LIMIT 50";
        $arr = $db->getAll($sql);
    }

    if (empty($arr))
    {
        $arr = array( 0 => array(
                     'id'   => 0,
                     'name' => '没有找到相应记录，请重新搜索'
        ));
    }

    make_json_result($arr);
}elseif($_REQUEST['act'] == 'act_allot'){
	
	admin_priv('allot_agent_auth');
	
}

function get_agent_userlist(){	
    $result = get_filter();
    if ($result === false)
    {
        /* 过滤条件 */
        $filter['keywords'] = empty($_REQUEST['keywords']) ? '' : trim($_REQUEST['keywords']);
        if ($_REQUEST['is_ajax'] == 1)
        {
            $filter['keywords'] = json_str_iconv($filter['keywords']);
        }

        $filter['sort_by']    = empty($_REQUEST['sort_by'])    ? 'user_id' : trim($_REQUEST['sort_by']);
        $filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC'     : trim($_REQUEST['sort_order']);

//        $ex_where = ' WHERE union_status=2 AND (agent_status=1 OR agent_status=2) ';
        if ($filter['keywords'])
        {
            $ex_where .= " AND user_name LIKE '%" . mysql_like_quote($filter['keywords']) ."%'";
        }     

        $filter['record_count'] = $GLOBALS['db']->getOne("SELECT COUNT(*) FROM " . $GLOBALS['os']->table('users') . $ex_where);

        /* 分页大小 */
        $filter = page_and_size($filter);
        $sql = "SELECT user_id, user_name, email, sex, office_phone, home_phone, mobile_phone, reg_time ".
                " FROM " . $GLOBALS['os']->table('users') . $ex_where .
                " ORDER by " . $filter['sort_by'] . ' ' . $filter['sort_order'] .
                " LIMIT " . $filter['start'] . ',' . $filter['page_size'];

        $filter['keywords'] = stripslashes($filter['keywords']);
        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }

    $user_list = $GLOBALS['db']->getAll($sql);

    $count = count($user_list);
    for ($i=0; $i<$count; $i++)
    {
 		$sex_list   =  array( 0 =>'保密', 1 => '男', 2 => '女');
    	$user_list[$i]['sex_format'] = in_array(intval($user_list[$i]['sex']), array(0, 1, 2)) ? $sex_list[$user_list[$i]['sex']] : 0;   
        $user_list[$i]['reg_time'] = local_date($GLOBALS['_CFG']['date_format'], $user_list[$i]['reg_time']);
    }

    $arr = array('user_list' => $user_list, 'filter' => $filter,
        'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);

    return $arr;	
}
?>